﻿/*
创建人：zhangrj
日期：2022-01-04
描述：批次工序温度维护页面js
*/
var page = {
    rootPath: getRootPath(),
    pageType: getParam("type"),
    init: function () {
        page.bind();
        if (isNullOrWhiteSpaceStr(page.pageType)) {
            page.bindContainerTable(); //绑定批次信息表            
        }
        if (page.pageType == "1") {
            $("#dvContainerSearch").css("display", "block");
        }
    },
    bind: function () {
        //批次列表
        $("#gdContainerInfo").datagrid({
            columns: [[
		         { field: 'CONTAINERNAME', title: '批次', width: 180 },
                 { field: 'PRODUCTNAME', title: '产品信息', width: 150 },
                 { field: 'QTY', title: '数量', width: 60 },
                 { field: 'WORKFLOWNAME', title: '工艺路线', width: 180 },
                  { field: 'WORKFLOWREVISION', title: '工艺版本', width: 80 },
                 { field: 'WORKFLOWID', title: '', width: 10, hidden: true },
                  { field: 'CONTAINERID', title: '', width: 10, hidden: true },
                ]],
            onClickRow: function (rowIndex, rowData) {
                page.bindWorkFlowStepTable();
            }
        });
        //工序列表
        $("#gdWorkFlowStepInfo").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		         { field: 'SPECNO', title: '工序号', width: 100 },
                 { field: 'SPECNAME', title: '工序名', width: 350 },
                   { field: 'OPDESC', title: '', width: 170, hidden: true },
                  { field: 'YPContainerTemperatureInfoID', title: '', width: 200, hidden: true }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.bindStepTem(rowData.SPECNO);
            },
            onUnselect: function (rowIndex, rowData) {
                $("#gdWorkFlowStepInfo").datagrid("uncheckAll");
                page.clearChecked();
            }
            //            onClickRow: function (rowIndex, rowData) {
            //                page.bindStepTem(rowData.SPECNO);
            //            }
        });
        //清除按钮
        $("#btnReset").on('click', function () {
            page.resetData(true, true, true);
        });
        //搜索
        $("#btnSearch").on('click', function () {
            page.searchData();
        });
        //保存
        $('#btnSaveData').on('click', function () {
            $('#btnSaveData').linkbutton('disable'); //禁用按钮
            var crow = $("#gdContainerInfo").datagrid("getSelected");
            if (crow === undefined || crow === null) {
                displayMessage2("未选择批次", false, ["btnSaveData"]);
                return;
            }
            var rows = $("#gdWorkFlowStepInfo").datagrid('getChecked');
            if (rows.length == 0) {
                displayMessage2("未选择工序", false, ["btnSaveData"]);
                return;
            }
            var stepData = [];
            var temData = [];
            var isTiTem = false;
            $('#checkBoxList').find('.easyui-checkbox').each(function () {
                var checkObj = $(this);
                if (checkObj.checkbox("options").checked) {
                    var txtId = checkObj.attr("data-txtid");
                    if (txtId == "txtTemShenQian" || txtId == "txtTemNoMaint") { //如果是温度审签中或者温度不维护
                        temData.push({ "TemName": checkObj.val(), "TemValue": checkObj.val() });
                    } else {
                        var vals = $('#' + txtId).val();
                        if ($.trim(vals) == "") {
                            isTiTem = true;
                            return;
                        }
                        temData.push({ "TemName": checkObj.val(), "TemValue": vals + "℃" });
                    }
                }
            });
            if (isTiTem) {
                displayMessage2("未填写温度数据", false, ["btnSaveData"]);
                return;
            }
            if (temData.length == 0) {
                displayMessage2("未勾选温度数据", false, ["btnSaveData"]);
                return;
            }
            for (var i = 0; i < rows.length; i++) {
                stepData.push({ "ContainerId": crow.CONTAINERID, "SpecNo": rows[i].SPECNO, "SpecDesc": rows[i].OPDESC });
            }
            var formData = {
                action: "Save",
                stepData: JSON.stringify(stepData),
                temData: JSON.stringify(temData)
            };
            $.ajax({
                type: "Post",
                url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
                data: formData,
                cashe: false,
                success: function (data) {
                    var result = JSON.parse(data);
                    if (result.Result) {
                        $("#gdWorkFlowStepInfo").datagrid('uncheckAll');
                        page.clearChecked();
                        page.bindWorkFlowStepTable();
                        parent.window.returnValue = { page: 'YPContaienrTemperaturePage.html', isSuccess: true };
                        displayMessage2(result.Message, true, ["btnSaveData"]);
                        return;
                    }
                    displayMessage2(result.Message, false, ["btnSaveData"]);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    displayMessage2(XMLHttpRequest.responseText, false, ["btnSaveData"]);
                }
            });
        });
        //清除
        $('#btnClearData').on('click', function () {
            $('#btnClearData').linkbutton('disable'); //禁用按钮
            var stepData = [];
            var crow = $("#gdContainerInfo").datagrid("getSelected");
            if (crow === undefined || crow === null) {
                displayMessage2("未选择批次", false, ["btnSaveData"]);
                return;
            }
            var rows = $("#gdWorkFlowStepInfo").datagrid('getChecked');
            if (rows.length == 0) {
                displayMessage2("未选择工序", false, ["btnClearData"]);
                return;
            }
            for (var i = 0; i < rows.length; i++) {
                stepData.push({ "ContainerId": crow.CONTAINERID, "SpecNo": rows[i].SPECNO, "SpecDesc": rows[i].OPDESC });
            }
            var formData = {
                action: "Clear",
                stepData: JSON.stringify(stepData)
            };
            $.ajax({
                type: "Post",
                url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
                data: formData,
                cashe: false,
                success: function (data) {
                    var result = JSON.parse(data);
                    if (result.Result) {
                        page.clearChecked();
                        parent.window.returnValue = { page: 'YPContaienrTemperaturePage.html', isSuccess: true };
                    }
                    displayMessage2(result.Message, result.Result, ["btnClearData"]);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    displayMessage2(XMLHttpRequest.responseText, false, ["btnClearData"]);
                }
            });
        })
    },
    //获取批次数据
    bindContainerTable: function () {
        var ycontainerId = getParam("containerId");
        var yworkflowId = getParam("workflowId");
        var formData = {
            action: "GetContainerList",
            containerId: ycontainerId,
            workflowId: yworkflowId
        };
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
            data: formData,
            cashe: false,
            success: function (data) {
                $("#gdContainerInfo").datagrid('loaded');
                var result = JSON.parse(data);
                $("#gdContainerInfo").datagrid('loadData', { rows: result.DBTable });
                if (result.DBTable.length > 0) {
                    $("#gdContainerInfo").datagrid('selectRow', 0);
                    page.bindWorkFlowStepTable();
                }
                displayMessage("查询完成", true);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    resetData: function (iscondition, isresult, isdetail) {
        clearMessage();
        $("#TemMaintInfo").html("");
        if (iscondition) {
            $('#txtMfgOrder').textbox('clear');
            $('#txtContainer').textbox('clear');
        }
        if (isresult) {
            clearEasyuiDataGrid("gdContainerInfo", 1);
            clearEasyuiDataGrid("gdWorkFlowStepInfo", 1);
        }
        if (isdetail) {
            page.clearChecked();
        }
    },
    //查询按钮查询数据
    searchData: function () {
        page.resetData(false, true, true);

        var formData = {
            action: "GetContainerListBySearch",
            mfgorderName: $("#txtMfgOrder").textbox('getValue'),
            containerName: $("#txtContainer").textbox('getValue'),
            status: $("#selStatus").combobox('getValue')
        };
        //如果没有状态，且没有输入查询条件，则提示
        if (formData.status === "0" && isNullOrWhiteSpaceStr(formData.mfgorderName) && isNullOrWhiteSpaceStr(formData.containerName)) {
            displayMessage("请输入查询条件", false);
            return;
        }
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
            data: formData,
            cashe: false,
            beforeSend: function () {
                $("#gdContainerInfo").datagrid('loading');
            },
            success: function (data) {
                $("#gdContainerInfo").datagrid('loaded');
                var result = JSON.parse(data);
                $("#gdContainerInfo").datagrid('loadData', { rows: result.DBTable });
                displayMessage("查询完成", true);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#gdContainerInfo").datagrid('loaded');
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    //获取工序列表数据
    bindWorkFlowStepTable: function () {
        clearEasyuiDataGrid("gdWorkFlowStepInfo", 1); //清空列表
        var row = $("#gdContainerInfo").datagrid("getSelected");
        var workformData = {
            action: "GetSpecList",
            workFlowId: row.WORKFLOWID,
            containerId: row.CONTAINERID
        };
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
            data: workformData,
            cashe: false,
            success: function (data) {
                $("#gdWorkFlowStepInfo").datagrid('loaded');
                var result = JSON.parse(data);
                $("#gdWorkFlowStepInfo").datagrid('loadData', { rows: result.DBTable });
                $("#TemMaintInfo").html(result.ReturnData);
                displayMessage("查询完成", true);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    //通过工序点击获取温度数据
    bindStepTem: function (specNo) {
        page.clearChecked();
        var row = $("#gdContainerInfo").datagrid("getSelected");
        var steoformData = {
            action: "GetStepTemInfo",
            containerId: row.CONTAINERID,
            specNo: specNo
        };
        $.ajax({
            type: "Post",
            url: page.rootPath + "/Ashx/YePianContainer/YPContaienrTemperatureHandler.ashx",
            data: steoformData,
            cashe: false,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.DBTable != null && result.DBTable.length > 0) {
                    var resdata = result.DBTable;
                    $('#checkBoxList').find('.easyui-checkbox').each(function () {
                        var checkboxobj = $(this);
                        var name = checkboxobj.val();
                        for (var i = 0; i < resdata.length; i++) {
                            if (resdata[i].TEMPERATURENAME == name) {
                                page.setChecked(checkboxobj, resdata[i].TEMPERATUREVALUE);
                            }
                        }
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    //设置选中并文本赋值
    setChecked: function (obj, val) {
        obj.checkbox({ checked: true });
        var txtId = obj.attr("data-txtid");
        if (txtId != "txtTemShenQian" && txtId != "txtTemNoMaint") {
            $('#' + txtId).textbox({ value: val });
        }
    },
    //清空复选框
    clearChecked: function () {
        $('#checkBoxList').find('.easyui-checkbox').each(function () {
            $(this).checkbox({ checked: false });
            var txtId = $(this).attr("data-txtid");
            $('#' + txtId).textbox({ value: "" });
        });
    }
}
page.init();